package utils

import (
	"github.com/gin-gonic/gin"
)

type Response struct {
	// 只能大写
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data,omitempty"`
	Err  string      `json:"error,omitempty"`
}

type TraceErrorResponse struct {
	Response
	TrackID string `json:"track_id"`
}

func NoLoginErr() Response {
	return Response{
		Code: CodeNoLoginError,
		Msg:  "未登陆",
	}
}

func NoPermissionErr() Response {
	return Response{
		Code: CodeNoPermissionError,
		Msg:  "没有权限",
	}
}

func Err(code int, msg string, err error) Response {
	res := Response{
		Code: code,
		Msg:  msg,
	}

	// 生产环境隐藏异常
	if err != nil && gin.Mode() != gin.ReleaseMode {
		res.Err = err.Error()
	}

	return res
}


func DBErr(msg string, err error) Response {
	if msg == "" {
		msg = "数据库错误"
	}
	return Err(CodeDBError, msg, err)
}

func ParamErr(msg string, err error) Response {
	if msg == "" {
		msg = "参数错误"
	}
	return Err(CodeParamError, msg, err)
}

func EncryptErr(msg string, err error) Response {
	if msg == "" {
		msg = "加密错误"
	}
	return Err(CodeEncryptError, msg, err)
}
